package com.adui.mgt.performance.api;

import com.adui.mgt.performance.common.Result;
import com.adui.mgt.performance.entity.Course;
import com.adui.mgt.performance.entity.Subject;
import com.adui.mgt.performance.entity.SubjectCourse;
import com.adui.mgt.performance.entity.User;
import com.adui.mgt.performance.service.CourseService;
import com.adui.mgt.performance.service.SubjectCourseService;
import com.adui.mgt.performance.service.SubjectService;
import com.adui.mgt.performance.service.UserService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: 专业crud + 院系课程维护 + 人员专业维护
 **/
@RestController
@RequestMapping("/api/subjects")
public class SubjectApi {

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private SubjectCourseService subjectCourseService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return Result.data(subjectService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestBody Subject subject) {
        return subjectService.findPage(subject);
    }

    @PostMapping("/")
    public Result add(@RequestBody Subject subject) {
        subjectService.getBaseMapper().insert(subject);
        return Result.ok();
    }

    @PutMapping("/")
    public Result update(@RequestBody Subject subject) {
        subjectService.getBaseMapper().updateById(subject);
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        subjectService.removeById(id);
        return Result.ok();
    }

    /**
     * 给专业加课程
     * @param subjectId
     * @param courseIds
     * @return
     */
    @PostMapping("/courses/{subjectId}")
    public  Result putCourse2Subject(@PathVariable("subjectId") String subjectId, List<String> courseIds) {

        List<SubjectCourse> subjectCourses = new ArrayList<>();
        for (String courseId : courseIds) {
            SubjectCourse subjectCourse = new SubjectCourse();
            subjectCourse.setSubjectId(subjectId);
            subjectCourse.setCourseId(courseId);
            subjectCourses.add(subjectCourse);
        }
        subjectCourseService.saveOrUpdateBatch(subjectCourses);
        return Result.ok();
    }

    @GetMapping("/courses/{subjectId}")
    public Result findCousrseBySubject(@PathVariable("subjectId") String subjectId) {

        List<SubjectCourse> subjectCourses = subjectCourseService.list(Wrappers.<SubjectCourse>lambdaQuery().eq(SubjectCourse::getSubjectId, subjectId));
        Set<String> courseIds = subjectCourses.stream().map(SubjectCourse::getCourseId).collect(Collectors.toSet());
        List<Course> courses = courseService.listByIds(courseIds);
        Map<String, Course> courseMap = courses.stream().collect(Collectors.toMap(Course::getId, Function.identity()));
        for (SubjectCourse s : subjectCourses) {
            Course course = courseMap.get(s.getCourseId());
            if (Objects.isNull(course)) {
                continue;
            }
            s.setCourseName(course.getCourseName());
            s.setCourseCode(course.getCourseCode());
        }
        return Result.data(subjectCourses);
    }


    /**
     * 给人加专业
     * @param id
     * @param userIds
     * @return
     */
    @PostMapping("/users/{id}")
    public Result addMembers2Subject(@PathVariable("id") String id, @RequestBody List<String> userIds) {
        List<User> users = userService.listByIds(userIds);
        users.forEach(user -> user.setSubjectId(id));
        userService.updateBatchById(users);
        return Result.ok();
    }
}
